#ifndef HELMETINPUTS_H
#define HELMETINPUTS_H

#include "HelmetSystemConfig.h"
#include "HelmetOutputs.h"

/**************************************************************************************
	Global Variables
**************************************************************************************/
unsigned int LogHangTime = 0; // Not logging hang-time
unsigned short int PrevButtonRed = 0; // button Red unpressed previously
unsigned short int PrevButtonGrn = 0;
unsigned short int PrevButtonBlu = 0;

/**************************************************************************************
	Definitions
**************************************************************************************/
// Accelerometer zero-g detect
// Port B bit 15 is also used by ADC channel 15
#define Read0GD() mPORTBReadBits( BIT_15 ) // read the 0-g detect from accelerometer
#define Config0GD() mPORTBSetPinsDigitalIn( BIT_15 )

// Remote Button Pad
#define PORTB_REM_BUTN_MASK		( BIT_5 | BIT_4 | BIT_3 | BIT_2 )
#define ConfigRemoteButtons()	mPORTBSetPinsDigitalIn( PORTB_REM_BUTN_MASK )
#define GetButtonCode()			(( mPORTBRead() & 0x003C ) >> 2 )

// Attached Button Pad
#define PORTA_ATT_BUTN_MASK		( BIT_1 )
#define PORTF_ATT_BUTN_MASK		( BIT_13 | BIT_12 )
#define ConfigAttachedButtons()	mPORTASetPinsDigitalIn( PORTA_ATT_BUTN_MASK ); \
								mPORTFSetPinsDigitalIn( PORTF_ATT_BUTN_MASK )

// use pins 76-84 for button inputs
#define PORTD_PB_MASK	( BIT_1 | BIT_2 | BIT_3 | BIT_12 | BIT_13 | BIT_4 | BIT_5 | BIT_6 | BIT_7 )
#define ConfigButtons()	mPORTDSetPinsDigitalIn( PORTD_PB_MASK )
#define BUTTON1			BIT_1
#define BUTTON2			BIT_2
#define BUTTON3			BIT_3
#define BUTTON4			BIT_12
#define BUTTON5			BIT_13
#define BUTTON6			BIT_4
#define BUTTON7			BIT_5
#define BUTTON8			BIT_6
#define BUTTON9			BIT_7
#define ButtonIsPressed()	( mPORTDRead() & 0x30FE )
#define Button1Pressed()	( mPORTDReadBits( BUTTON1 ) )
#define Button2Pressed()	( mPORTDReadBits( BUTTON2 ) )
#define Button3Pressed()	( mPORTDReadBits( BUTTON3 ) )
#define Button4Pressed()	( mPORTDReadBits( BUTTON4 ) )
#define Button5Pressed()	( mPORTDReadBits( BUTTON5 ) )
#define Button6Pressed()	( mPORTDReadBits( BUTTON6 ) )
#define Button7Pressed()	( mPORTDReadBits( BUTTON7 ) )
#define Button8Pressed()	( mPORTDReadBits( BUTTON8 ) )
#define Button9Pressed()	( mPORTDReadBits( BUTTON9 ) )

/**************************************************************************************
	Function Prototypes
**************************************************************************************/
void ConfigInputs( void );
char GetRemoteButtonPressed();

/**************************************************************************************
	Functions
**************************************************************************************/
void ConfigInputs() {
	// Config accelerometer zero-g detect
	Config0GD();
	ConfigRemoteButtons();
	// Config buttons
	// ConfigButtons();
}	

void CheckPushbuttons() {
	// If in free fall, increment hang time counter
    if( Read0GD() ) {
	    LogHangTime++;
	}
	// If not in free fall and hang time has been counted,
	// tell main to report the hang time
	else if( LogHangTime > 0 ) {
		DoInMain( ReportHangTime ); // report hang time
	}
	
	if( ((mPORTAReadBits(PORTA_ATT_BUTN_MASK) >> 1) ^ 0xFFFF )& 0x0001 || (( mPORTFReadBits(PORTF_ATT_BUTN_MASK) >> 12 ) ^ 0xFFFF ) & 0x0003 ) {
		DoInMain( CheckAttachedButtonPad );
		ButtonLEDsOn();
		VibMotorOn();
	} else {
		prevButton = 0;
		ButtonLEDsOff();
		VibMotorOff();
	}	
}

char GetAttachedButtonPressed() {
	unsigned short int code = mPORTAReadBits( PORTA_ATT_BUTN_MASK ) << 1;
	code = code | mPORTFReadBits( PORTF_ATT_BUTN_MASK ) >> 12 & 0x0003;
	code = ( code ^ 0xFFFF ) & 0x0007;
	switch( code ) {
		case 0x00 : return( '0' );
		case 0x01 : return( 'U' );
		case 0x02 : return( 'D' );
		case 0x03 : return( 'R' );
		case 0x04 : return( 'M' );
		case 0x05 : return( '0' );
		case 0x06 : return( 'L' );
		case 0x07 : return( '0' );
		default : return( '0' );	
	}
	return( 0 );
}	

char GetRemoteButtonPressed() {
	unsigned short int code = GetButtonCode();
	switch( code ) {
		case 0x00 : return( '0' );
		case 0x01 : return( 'M' );
		case 0x02 : return( 'D' );
		case 0x03 : return( 'L' );
		case 0x04 : return( 'U' );
		case 0x05 : return( '0' );
		case 0x06 : return( 'R' );
		case 0x07 : return( '0' );
		default : return( '0' );	
	}
	return( 0 );
}	

float GetHangTime() {
	float actht = ( LogHangTime*1.0 - 1 ) / T1_FREQ ; // hangtime = logged hangtime / frequency of T1 interrupt
	LogHangTime=0;
	return actht;
}

#endif